﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace nicotool
{
    /// <summary>
    /// 动画详细信息
    /// </summary>
   public class ThumbInfo
    {
       /// <summary>
       /// 动画id
       /// </summary>
        public string  id { get; set; }

       /// <summary>
       /// 标题
       /// </summary>
        public string title { get; set; }

       /// <summary>
       /// 说明
       /// </summary>
        public string Description { get; set; }

       /// <summary>
       /// 再生数
       /// </summary>
        public int  ViewCounter { get; set; }

       /// <summary>
       /// 弹幕数
       /// </summary>
        public int CommentNum { get; set; }

       /// <summary>
       /// 预览路径
       /// </summary>
        public string ThumbnailUrl { get; set; }

       /// <summary>
       /// 标签
       /// </summary>
        public string[] Tags { get; set; }

       /// <summary>
       /// 投稿日
       /// </summary>
        public DateTime FirstRetrieve { get; set; }

       /// <summary>
       /// 时间长度
       /// </summary>
        public string Lenght { get; set; }
       /// <summary>
       /// 格式
       /// </summary>
        public string  movieType { get; set; }
       /// <summary>
       /// 高清大小
       /// </summary>
        public long  sizeHigh { get; set; }
       /// <summary>
       /// 低画质大小
       /// </summary>
        public long sizeLow { get; set; }



        public static ThumbInfo Create(string id)
        {
            XDocument xml;
            try
            {
                 xml = XDocument.Load(StrUrl.getThumbInfo(id));
            }
            catch (Exception ex)
            {
                
                throw new Exception("获得ThumbInfo错误",ex);
            }

            if (xml.Element("nicovideo_thumb_response").Attribute("status").Value == "fail")
            {
                throw new Exception("视频ThumbInfo获取失败.");
            }

            var info = new ThumbInfo();
            try
            {
                info.title = xml.Descendants("title").First().Value;
                info.Description = xml.Descendants("description").First().Value;
                info.ThumbnailUrl = xml.Descendants("thumbnail_url").First().Value;
                info.id = id;
                info.ViewCounter = int.Parse(xml.Descendants("view_counter").First().Value);
                info.CommentNum = int.Parse(xml.Descendants("comment_num").First().Value);
                info.Tags = xml.Descendants("tags").Where(x => x.Attribute("domain").Value == "jp").Elements().Select(x => x.Value).ToArray();
                info.FirstRetrieve = DateTime.Parse(xml.Descendants("first_retrieve").First().Value);
                info.Lenght = xml.Descendants("length").First().Value;
                info.sizeHigh = long.Parse(xml.Descendants("size_high").First().Value);
                info.sizeLow = long.Parse(xml.Descendants("size_low").First().Value);
                info.movieType = xml.Descendants("movie_type").First().Value;
            }
            catch (Exception ex)
            {
                
                throw new Exception("获取ThumbInfo 参数失败",ex);
            }
            return info;
        }
    }
}
